package com.university.mentalhealthplatform.teacherV2.controller;

import com.university.mentalhealthplatform.appModels.FamilyMessage;
import com.university.mentalhealthplatform.teacherV2.service.FamilyMessageServiceV2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/familymessage")
public class FamilyMessageControllerV2 {

    @Autowired
    private FamilyMessageServiceV2 familyMessageServiceV2;

    @PostMapping
    public ResponseEntity<FamilyMessage> createFamilyMessage(@RequestBody FamilyMessage familyMessage) {
        FamilyMessage savedFamilyMessage = familyMessageServiceV2.saveFamilyMessage(familyMessage);
        return ResponseEntity.ok(savedFamilyMessage);
    }

    @GetMapping("/{id}")
    public ResponseEntity<FamilyMessage> getFamilyMessageById(@PathVariable Long id) {
        FamilyMessage familyMessage = familyMessageServiceV2.getFamilyMessageById(id);
        return familyMessage != null ? ResponseEntity.ok(familyMessage) : ResponseEntity.notFound().build();
    }

    @GetMapping
    public ResponseEntity<List<FamilyMessage>> getAllFamilyMessages() {
        List<FamilyMessage> familyMessages = familyMessageServiceV2.getAllFamilyMessages();
        return ResponseEntity.ok(familyMessages);
    }

    @PutMapping("/{id}")
    public ResponseEntity<FamilyMessage> updateFamilyMessage(@PathVariable Long id, @RequestBody FamilyMessage familyMessage) {
        familyMessage.setId(id);
        FamilyMessage updatedFamilyMessage = familyMessageServiceV2.updateFamilyMessage(familyMessage);
        return ResponseEntity.ok(updatedFamilyMessage);
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteFamilyMessage(@PathVariable Long id) {
        familyMessageServiceV2.deleteFamilyMessage(id);
        return ResponseEntity.noContent().build();
    }

    @GetMapping("/search")
    public ResponseEntity<List<FamilyMessage>> searchFamilyMessages(@RequestParam String keyword) {
        List<FamilyMessage> familyMessages = familyMessageServiceV2.searchFamilyMessages(keyword);
        return ResponseEntity.ok(familyMessages);
    }
}